package bme.andraskindler.treenventory.provider;

import java.util.Date;
import java.util.List;

import android.graphics.Color;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import bme.andraskindler.treenventory.R;
import bme.andraskindler.treenventory.fragment.SyncFragment;
import bme.andraskindler.treenventory.model.TreeData;

/**
 * Szabványos listaadapter, a {@link SyncFragment} használja.
 * @author andraskindler
 *
 */
public class SyncListAdapter extends BaseAdapter {

	private List<TreeData> mList;

	public SyncListAdapter() {
		mList = TreenventoryDataSource.getAllTrees();
	}

	public int getCount() {
		return mList.size();
	}

	public Object getItem(int position) {
		return mList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		TextView listItem = (TextView) convertView;

		if (listItem == null)
			listItem = (TextView) LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_treetype, null);

		final TreeData data = mList.get(position);

		String htmlString = data.getType() + "<br/><small>" + new Date(data.getDate()).toString() + "</small>";
		listItem.setText(Html.fromHtml(htmlString));
		listItem.setBackgroundColor(Color.LTGRAY);

		return listItem;
	}

	@Override
	public void notifyDataSetChanged() {
		mList = TreenventoryDataSource.getAllTrees();
		super.notifyDataSetChanged();
	}

}
